package gui;

import java.awt.Component;
import java.awt.Dimension;
import java.text.SimpleDateFormat;

import javax.swing.BoxLayout;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

import net.miginfocom.swing.MigLayout;
import actions.ListAction;
import actions.PrivateChatAction;
import model.User;

public class ChatTab extends JPanel {

	private User chatWith;
	private JTextArea textAreaMessages = new JTextArea();
	private JList list;
	/**
	 * Create the panel.
	 */
	public ChatTab() {
		super();
		init();
		setName("Public chat");
		//JPanel panel_1 = new JPanel();
		//panel_1.setLayout(new MigLayout("", "[454.00px][258px]", "[344.00px]"));
		JScrollPane scrollPane = new JScrollPane();
		//panel_1.add(scrollPane);
		
		list = new JList();
		list.setFixedCellWidth(150);
		ListAction la = new ListAction(list, new PrivateChatAction());
		scrollPane.setViewportView(list);
		Dimension d = list.getPreferredSize();
		d.width=180;
		d.height=400;
		//list.setMaximumSize(d);
		scrollPane.setMaximumSize(d);
		
		add(scrollPane,Component.RIGHT_ALIGNMENT);
	}
	
	public ChatTab(User chatWith) {
		super();
		init();
		this.chatWith=chatWith;
		setName("Chat with: "+chatWith.toString());
	}

	public void displayText(String text) {
		textAreaMessages.append(text);
	}
	
	public void displayMessage(model.Message message) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		
		String displayString="["+sdf.format(message.getDate())+"] ";
		displayString+="["+message.getFrom().getUsername()+"@"+message.getFrom().getHost().getAlias()+"]: ";
		displayString+=message.getContent();

		textAreaMessages.append(displayString+"\n");

	}

	private void init() {
		
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setViewportView(textAreaMessages);
		add(scrollPane);
		
		
		textAreaMessages.setEditable(false);
	}
	
	public User getChatWith() {
		return chatWith;
	}

	public void setChatWith(User chatWith) {
		this.chatWith = chatWith;
	}

	public JList getList() {
		return list;
	}

	public void setList(JList list) {
		this.list = list;
	}
}
